package main

import (
	"path/filepath";"os";"log";"fmt";
	"net/http"
	"jinquan.supor.com/echos"
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

func Assets_Directory() (string,string) {
	cur_dir,err := filepath.Abs(filepath.Dir(os.Args[0]))
	var assets_dir string
	if err != nil {
		log.Fatal(err)
	}else{
		assets_dir = cur_dir+"/"+os.Args[1]
		fmt.Printf("Assets working directory: %s\n", assets_dir)
	}
	return cur_dir,assets_dir
}

func accessible(c echo.Context) error {
	return c.String(http.StatusOK, "healthy")
}

func main() {
	bin_dir,assets_dir := Assets_Directory()
	e := echo.New()
	// Middleware
	e.Use(middleware.Logger())
	//e.Use(middleware.Recover())
	e.Static("/static", assets_dir) //静态资源分离

	e.GET("/healthy", accessible) //健康检查探针

	/* Prometheus 默认监控指标暴露 */
	e.GET("/metrics", echo.WrapHandler(promhttp.Handler()) )


	//初始化Redis中的全局参数
	echos.APP_Redis_Init()

	/* 具备Portal身份验证的web应用程序 */
	//HTML模板池初始化
  	echos.APP_TemplatesPool(e, bin_dir+"/"+"templates/*.html")

  	//Portal页面路由, POST方法, 无cookie, token
  	echos.APP_Portal_URL_Registry("/portal", e, echos.APP_CbFunc_Portal)
  	echos.APP_HttpClientAction_jsPostAuth_Registry("/jsPostAuth", e)

  	//注册页面路由, POST方法, 无cookie, token
  	echos.APP_Portal_URL_Registry("/regist", e, echos.APP_CbFunc_Regist)
  	echos.APP_HttpClientAction_jsPostRegist_Registry("/jsPostRegist", e)

  	//应用页面路由, GET方法, 自动上传cookie, token到Server端
	echos.APP_Common_URL_Registry("/mspapp1", e) 

	e.Logger.Fatal(e.Start(":1323"))
	
}
